﻿layui.use('table', function () {
    var table = layui.table;

    //监听工具条
    table.on('tool(btnClick)', function (obj) {
        var data = obj.data;
        if (obj.event === 'detail') {
            //layer.msg('ID：' + data.id + ' 的查看操作');
            active.edit(data.id);
        } else if (obj.event === 'del') {
            layer.confirm('确定删除？', function (index) {
                active.delete(data.id);
                layer.close(index);
                active.search();
            });
        } else if (obj.event === 'edit') {
            active.edit(data.id);
            //layer.alert('编辑行：<br>' + JSON.stringify(data))
        }
    });

    var $ = layui.$, active = {
         
        search: function () {
            //执行重载
             
            table.reload('tableReload', {
                 
                where: {
                    where: getQueryJson()
                }
            });
        },
        add: function () {
            if ($(this).hasClass('layui-btn-disabled')) {
                return;
            }
            parent.layer.open({
                type: 2,
                area: [winWidth, winHeight],
                maxmin: true,
                shade: 0,
                content: addUrl,
                end: function () {
                    active.search();
                }
            });
        },
        edit: function (id) {
            if ($(this).hasClass('layui-btn-disabled')) {
                return;
            }
            var url = editUrl + (editUrl.indexOf('?') > 0 ? '&' : '?') + "id=" + id;
            parent.layer.open({
                type: 2,
                area: [winWidth, winHeight],
                maxmin: true,
                shade: 0,
                content: url ,
                end: function () {
                    active.search();
                }
            });
        },
        delete: function (id) {
            var url = appUrl + "/delete";
            $.ajax({
                url: url,
                type: 'post',
                data: {
                    ids: id
                },
                success: function (res) {                    
                    layer.msg('删除成功！');
                    active.search();
                }
            })
        },
        deleteList: function () { //获取选中数据
            var checkStatus = table.checkStatus('tableReload')
                , data = checkStatus.data;
            if (checkStatus.data.length == 0) {
                return;
            }
            layer.msg('确定删除？', {
                time: 20000, //20s后自动关闭
                btn: ['确定', '取消'],
                yes: function () {
                    
                    var ids = "";
                    for (var i = 0; i < checkStatus.data.length; i++) {
                        ids += checkStatus.data[i].id + ";";
                    }
                    active.delete(ids);
                }
            });
            
        }
    };

    $('.search .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });




});